from rest_framework import serializers
# 导入Django内置的User模型
from django.contrib.auth.models import User

class RegisterSerializer(serializers.Serializer):
    username = serializers.CharField(min_length=5, max_length=12)
    password = serializers.CharField(min_length=5, max_length=12, write_only=True)

    def validate_username(self, value):
        if User.objects.filter(username=value).exists():
            raise serializers.ValidationError('用户名已存在')
        return value

    def create(self, validated_data):
        return User.objects.create_user(**validated_data)